Comprehensive guide for beginners and students
What is C?
Key Features:
Basic Program Structure:
#include <stdio.h> // Preprocessor directive
int main() {
printf("Hello, World!\n");
return 0;
}
| Data Type | Description | Size (bytes) | Format Specifier |
|---|---|---|---|
| int | Integer | 2 or 4 | %d or %i |
| float | Real Number (single-precision) | 4 | %f |
| double | Double-precision floating point | 8 | %lf |
| char | Single character | 1 | %c |
Variables: Declaration & Initialization
int age;
age = 30;
int roll = 101;
Constants:
#define PI 3.14159
const float pi = 3.14159;
Decision Making:
switch (expression) {
case 1:
// code
break;
default:
// code
}
Loops: for, while, do-while
for(int i=0; i<5; i++) {
printf("%d", i);
}
int add(int a, int b) { // Definition
return a + b;
}
int main() {
int sum = add(5, 3); // Call
printf("%d", sum);
return 0;
}
int numbers[5] = {1,2,3,4,5};
printf("%d", numbers[0]);
char name[20] = "John Doe";
printf("%s", name);
int x = 10;
int *ptr = &x;
printf("%d", *ptr);
struct Student {
int id;
char name[50];
float gpa;
};
struct Student s1;
s1.id = 101;
FILE *fp = fopen("data.txt", "w");
fprintf(fp, "Hello File!");
fclose(fp);